package xiu_xiu.utils.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.xiu_xiu.R;

import xiu_xiu.entity.PopupWindowSearchData;
import xiu_xiu.utils.ui.UiUtils;

/**
 * popupWindow 菜单的封装
 *
 * @author yang
 */
public class PopupWindowSearch extends PopupWindow implements OnClickListener {
    private View conentView;
    /**
     * 对话框点击事件
     */
    private OnClickCallback onClickCallback = null;
    private Context context;
    private PopupWindowSearchData data = new PopupWindowSearchData();

    private EditText date;
    private EditText orderNumber;
    private EditText carCompany;
    private EditText linkman;
    private EditText linkmanPhone;
    private EditText departAddress;
    private EditText goalAddress;


    /**
     * @param context 传入一个调入者对象（上下文菜单）
     */
    public PopupWindowSearch(final Context context) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        /** 得到视图 */
        conentView = inflater.inflate(R.layout.item_popup_filtrate, null);

        UiUtils.applyFont(conentView);

        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);

        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);

        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());

        // 刷新状态
        this.update();

        // 实例化一个ColorDrapWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview);
        this.setOutsideTouchable(true);

        initView();
    }


    /**
     * 对话框的显示位置
     *
     * @param parent 父控件
     */
    public void showPopupWindow(View parent) {
        UiUtils.setViewMeasure(conentView);
        if (!this.isShowing()) {
            setWidth(getScreenWidth(context));
            showAsDropDown(parent, 0, 0);
        } else {
            this.dismiss();
        }
    }

    /**
     * 初始化控件
     */
    private void initView() {
        //设置监听
//        conentView.findViewById(R.id.search_content).setOnClickListener(this);
    }

    /**
     * 给对象添加数据
     */
    private void addData() {

    }

    /**
     * 监听的处理
     * 实现接口并赋值
     */
    @Override
    public void onClick(View v) {
        if (onClickCallback != null) {
            addData();
            onClickCallback.onClick(data);
            dismiss();
        } else {
            dismiss();
        }
    }


    /**
     * 接口
     */
    public interface OnClickCallback {
        void onClick(PopupWindowSearchData data);

    }

    /**
     * 当设置监听时必须实现接口,通过接口参数把一些信息传递给调用者
     *
     * @param callback
     */
    public void setOnClickCallback(OnClickCallback callback) {
        onClickCallback = callback;
    }


    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕高度
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }













}
